package com.caunock.sqlbuilder.adapter.group.impl;

import com.caunock.sqlbuilder.meta.Column;
import com.caunock.sqlbuilder.meta.feature.AbstractDataType;
import com.caunock.sqlbuilder.meta.feature.ColumnType;

/**
 * Oracle适配器
 *
 * @author Qinweitong
 */
public class OracleAdapter extends PostgreSQLAdapter {

    @Override
    public StringBuilder increment(StringBuilder builder, Column<?> meta) {
        boolean autoIncrement = meta.isAutoIncrement();
        if (autoIncrement) {
            builder.append(" GENERATED BY DEFAULT AS IDENTITY");
            return builder;
        }
        return builder;
    }

    @Override
    public StringBuilder type(StringBuilder builder, Column<?> column) {
        ColumnType type = column.getType();
        AbstractDataType dataType = type.getDataType();
        int length = type.getLength();
        int scale = type.getScale();
        int precision = type.getPrecision();

        switch (dataType) {
            case STRING:
                if (length <= 4000) {
                    builder.append(" VARCHAR2(").append(length).append(")");
                } else {
                    builder.append(" CLOB");
                }
                break;
            case DECIMAL:
                builder.append(" NUMBER(").append(precision).append(", ").append(scale).append(")");
                break;
            case INTEGER:
                builder.append(" NUMBER(").append(Math.max(length, 38)).append(")");
                break;
            case DATE:
                builder.append(" DATE");
                break;
            case BINARY:
                builder.append(" BLOB");
                break;
            case CONTENT:
                builder.append(" CLOB");
                break;
            default:
                break;
        }
        return builder;
    }
}
